var Mrchao = function() {

    // 处理动态表头
    this.dynamic = function(data, Topdata) {
        // 定义两个接收尺码的数组
        var sizearr = [],
            sizearr0 = [],
            sizearr1 = [];
        // 向数组sizearr插入尺码
        for (var i = 0; i < data.length; i++) {
            var qty = JSON.parse(data[i].qty)
            for (item in qty) {
                data[i][item] = qty[item]
                sizearr.push(item)
            }
        }
        sizearr.sort() //排序
        sizearr0[0] = sizearr[0]
            // 向数组sizearr0插入尺码
        for (var i = 1; i < sizearr.length; i++) {
            if (sizearr[i] !== sizearr0[sizearr0.length - 1]) {
                sizearr0.push(sizearr[i])
            }
        }
        // 向JSON对象sizearr1j插入尺码
        for (var i = 0; i < sizearr0.length; i++) {
            var top = {
                field: sizearr0[i],
                filter: true,
                title: sizearr0[i],
                width: 60
            }
            sizearr1.push(top)
        }
        Topdata = Topdata.concat(sizearr1)
        return Topdata
    };


    //时间查询
    this.datecheck = function(data, checkbtn, val1, val2, date1, date2) {
        // 重新定义一个数组
        var data1 = []
            // 获取时间查询按钮
        $_$('#' + checkbtn).click(function() {
            data.sort(function(a, b) { return Date.parse(a[date1]) - Date.parse(b[date2]) })
                // 获取查询的时间段
            var test1 = Date.parse($_$('#' + val1).val())
            var test2 = Date.parse($_$('#' + val2).val())
            for (var i = 0; i < data.length; i++) {
                if (test1 <= Date.parse(data[i][date1]) && test2 >= Date.parse(data[i][date2])) {
                    data1.push(data[i])
                }
            }
        })
        return data1
    };


    // 搜索查询
    this.inputcheck = function(data, checkbtn, val1, checkid, FirstField, tableid) {
        // 获取制单号按钮
        $_$('#' + checkbtn).click(function() {
            var data1 = []
            var mpscheck = $_$('#' + val1).val();
            var Reg = new RegExp(mpscheck)
            for (var i = 0; i < data.length; i++) {
                if (Reg.test(data[i][checkid])) {
                    data1.push(data[i])
                }
            }
            this.Datecheck(data1, FirstField, tableid)
        })
    }


    // 渲染表格
    this.Datecheck = function(data, FirstField, tableid) {
        var strarr = []
            // 渲染除第一个表格的所有表格（包括表头）
        for (var i = 0; i < data.length; i++) { // 遍历表格的行
            strarr[0] += '<tr>'
            for (item in data[0]) {
                if (item == FirstField) { // 遍历每行的属性
                    strarr[0] += '<td>' + i + '</td><td>' + data[i][item] + '</td>'
                } else if (data[i][item] == undefined) {
                    strarr[0] += '<td></td>'
                } else {
                    strarr[0] += '<td>' + data[i][item] + '</td>'
                }
            } +
            '</tr>'
        }
        // 删除上次的表格
        $_$('#' + tableid).empty()
            // 重新载入表格
        $_$('#' + tableid).append(strarr[0]);

        // 销毁所有插入的表头
        // for (var i = 0; i < data.length; i++) {
        //     if (data[i].cutid == "裁单号") {
        //         data.splice(i, 1)
        //     }
        // }
        // console.log(data)
    }

    // 字符串转换成数组并且相加
    this.arrayadd = function(data, str) {
        var qtyarr = [] //接收相加的数
        for (var i = 0; i < data.length; i++) {
            var qty = JSON.parse(data[i][str])
            qtyarr[i] = 0
            for (item in qty) {
                qtyarr[i] = qtyarr[i] + qty[item]
            }
        }
        return qtyarr;
    }

    // 获取递减月份
    this.arrMonth = function() {
        var date = new Date()
        var time = [] //返回time1,time2
        var time1 = [], //接收时间
            time2 = [], //接收时间
            time3 = [],
            count = 0; //计算器+1
        for (var i = 0; i < 7; i++) {
            if ((date.getMonth() + 1 - count) <= 0) {
                time1.unshift((date.getFullYear() - 1) + '-' + (date.getMonth() + 13 - count) + '-' + 1)
                time3.unshift((date.getFullYear() - 1) + '-' + (date.getMonth() + 13 - count))
            } else {
                time1.unshift(date.getFullYear() + '-' + (date.getMonth() + 1 - count + '-' + 1))
                time3.unshift(date.getFullYear() + '-' + (date.getMonth() + 1 - count))
            }
            if ((date.getMonth() + 2 - count) <= 0) {
                time2.unshift((date.getFullYear() - 1) + '-' + (date.getMonth() + 14 - count) + '-' + 1)
            } else {
                time2.unshift(date.getFullYear() + '-' + (date.getMonth() + 2 - count + '-' + 1))
            }
            count = count + 1
        }
        time = [time1, time2, time3]
        return time
    }

    this.sql = function() {
        var time = this.arrMonth();
        var a = "select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][0] + "' AND '" + time[1][0] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][1] + "' AND '" + time[1][1] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][2] + "' AND '" + time[1][2] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][3] + "' AND '" + time[1][3] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][4] + "' AND '" + time[1][4] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][5] + "' AND '" + time[1][5] + "' AND is_order='2')"
        a = a + " UNION ALL"
        a = a + " select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE creation_date BETWEEN '" + time[0][6] + "' AND '" + time[1][6] + "' AND is_order='2')"
        return a;
    }


}